package com.dragee.modules.officialWebsite.controller;

import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.modules.officialWebsite.entity.GwFriendLinkEntity;
import com.dragee.modules.officialWebsite.service.GwFriendLinkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;



/**
 * 友情链接
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-07-11 22:54:09
 */
@RestController
@RequestMapping("officialWebsite/gwfriendlink")
@Api(tags = "友情链接")
public class GwFriendLinkController {
    @Autowired
    private GwFriendLinkService gwFriendLinkService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("友情链接列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = gwFriendLinkService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("友情链接信息")
    public R info(@PathVariable("id") Long id){
		GwFriendLinkEntity gwFriendLink = gwFriendLinkService.getById(id);
        return R.ok().put("data", gwFriendLink);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改友情链接")
    public R saveOrUpdate(@RequestBody GwFriendLinkEntity gwFriendLink){
        gwFriendLinkService.saveOrUpdate(gwFriendLink);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("友情链接删除")
    public R delete( String id){
		gwFriendLinkService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("友情链接批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        Arrays.stream(idArr).forEach(id -> gwFriendLinkService.removeById(Long.parseLong(id)));
        return R.ok();
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("/import")
    public R importData(@RequestParam("file") MultipartFile file) throws IOException {
        return gwFriendLinkService.importData(file);
    }

    @ApiOperation(value = "数据导出")
    @GetMapping("/export")
    public void exportData(@RequestParam Map<String, Object> params, HttpServletResponse response ) throws Exception {
            gwFriendLinkService.exportData(params, response);
    }

}
